package com.bytezero.menu;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    public static final String TAG = "MainActivity";
    private String name[] = {"zhenglei", "jian", "li", "zhangsan", "lisi"};
    private List<Fruit> fruitList = new ArrayList<>();

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;


    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add:
                Toast.makeText(MainActivity.this, "add++", Toast.LENGTH_SHORT).show();
                break;
            case R.id.remove:
                Toast.makeText(MainActivity.this, "remove++", Toast.LENGTH_LONG).show();
                break;
            default:
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String returnData = data.getStringExtra("data");
                    Log.e(TAG, "onActivityResult: " + returnData);

                }
                break;
            default:
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//
//        if (savedInstanceState != null) {
//            String tempData = savedInstanceState.getString("data_key");
//            Log.d(TAG, tempData);
//        }
        initFruits();

        if (savedInstanceState != null) {
            String data = savedInstanceState.getString("name");
            Log.e(TAG, "onCreate: " + data);
        }

        Button button = (Button) findViewById(R.id.but_1);
        button.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setData(Uri.parse("http://www.baidu.com"));
            startActivity(intent);
        });

        Button button2 = (Button) findViewById(R.id.but_2);
        button2.setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:10086"));
            startActivity(intent);
        });


        Button button3 = (Button) findViewById(R.id.but_3);
//       button3.setOnClickListener(v->{
//           String data = "zhenglei";
//           Intent intent = new Intent(MainActivity.this, MainActivity2.class);
//           intent.putExtra("name",data);
//           startActivity(intent);
//       });

//        button3.setOnClickListener(v -> {
//            int data = 123;
//            Intent intent = new Intent(MainActivity.this, MainActivity2.class);
//            intent.putExtra("age", data);
//            startActivity(intent);
//        });

        button3.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, MainActivity2.class);
            startActivityForResult(intent, 1);
        });

        FruitAdapter fruitAdapter = new FruitAdapter(MainActivity.this, R.layout.fruit_item, fruitList);
      //  ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, name);
        ListView listView = (ListView) findViewById(R.id.lv_1);
        listView.setAdapter(fruitAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fruit fruit = fruitList.get(position);
                Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_LONG).show();
            }
        });





    }

    private void initFruits() {
        for (int i = 0; i < 12; i++) {
            Fruit apple = new Fruit("apple", R.drawable.ic_launcher_background);
            fruitList.add(apple);
            Fruit banana = new Fruit("banana", R.drawable.ic_launcher_foreground);
            fruitList.add(banana);
        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        String data = "zhenglei+++++++++++++++++++";
        outState.putString("name", data);
    }
}